package cn.jbit.easybuy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.jbit.easybuy.dao.BaseDao;
import cn.jbit.easybuy.dao.ProductDao;
import cn.jbit.easybuy.entity.Page;
import cn.jbit.easybuy.entity.Product;
import cn.jbit.easybuy.entity.ProductCategory;

public class ProductDaoImpl extends BaseDao implements ProductDao {

	@Override
	public Page findAll(int pageNo, int pageSize) {
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Product> list = new ArrayList<Product>();
		Page page = new Page();
		page.setCurrPageNo(pageNo);
		page.setPageSize(pageSize);
		try {
			String sql = "select * from easybuy_product limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (pageNo-1)*pageSize);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Product product = new Product();
				product.setId(rs.getLong("ep_id"));
				product.setName(rs.getString("ep_name"));
				product.setDescription(rs.getString("ep_description"));
				product.setPrice(rs.getFloat("ep_price"));
				product.setStock(rs.getLong("ep_stock"));
				product.setCategoryId(rs.getLong("epc_id"));
				product.setChildCategoryId(rs.getLong("epc_child_id"));
				product.setFileName(rs.getString("ep_file_name"));
				list.add(product);
			}
			page.setDataList(list);
			//查询总记录数
			String countSql = "select count(1) from easybuy_product";
			ps = conn.prepareStatement(countSql);
			rs = ps.executeQuery();
			if (rs.next()) {
				int count = rs.getInt(1);
				page.setTotalCount(count);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(conn, ps, rs);
		}
		return page;
	}

	@Override
	public Page findByCategory(int pageNo, int pageSize, long categoryId) {
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Product> list = new ArrayList<Product>();
		Page page = new Page();
		page.setCurrPageNo(pageNo);
		page.setPageSize(pageSize);
		try {
			String sql = "select * from easybuy_product where epc_child_id = ? limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setLong(1, categoryId);
			ps.setInt(2, (pageNo-1)*pageSize);
			ps.setInt(3, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Product product = new Product();
				product.setId(rs.getLong("ep_id"));
				product.setName(rs.getString("ep_name"));
				product.setDescription(rs.getString("ep_description"));
				product.setPrice(rs.getFloat("ep_price"));
				product.setStock(rs.getLong("ep_stock"));
				product.setCategoryId(rs.getLong("epc_id"));
				product.setChildCategoryId(rs.getLong("epc_child_id"));
				product.setFileName(rs.getString("ep_file_name"));
				list.add(product);
			}
			page.setDataList(list);
			String countSql = "select count(1) from easybuy_product where epc_child_id = ?";
			ps = conn.prepareStatement(countSql);
			ps.setLong(1, categoryId);
			rs = ps.executeQuery();
			if (rs.next()) {
				int count = rs.getInt(1);
				page.setTotalCount(count);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(conn, ps, rs);
		}
		return page;
	}

}
